package gw.com.android.ui.chart;

import android.content.res.Resources;

import com.gwtsz.chart.output.utils.ChartConfig;
import com.gwtsz.gts2.hx.R;

import org.json.JSONArray;

import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;

/**
 * 图表类用到的数组文案
 */
public class ChartTypeData {
    private static ChartTypeData mInstance = null;
    private String[] mChartSupIndicator = null;//主图指标
    private String[] mChartInfIndicator = null;//副图指标
    private String[] mLandPeriodNames = null; //日期周期文案数组
    private String[] mPortPeriodNames = null; //日期周期文案数组
    private String[] mChartAllPeriodNames = null;
    private DataItemResult mPopPeriodNames = null; //日期周期弹层文案
    private DataItemResult mMenuNames = null; //图表菜单

    /**
     * 获取此类单例对象
     */
    public static ChartTypeData instance() {
        if (null == mInstance) {
            mInstance = new ChartTypeData();
        }
        return mInstance;
    }

    /**
     * 重置图表弹层数据
     */
    public static void reset() {
        mInstance = null;
    }

    /**
     * 初始化类数据
     */
    private ChartTypeData() {
        Resources res = AppMain.getApp().getResources();
        mLandPeriodNames = res.getStringArray(R.array.chart_land_period_names);
        mPortPeriodNames = res.getStringArray(R.array.chart_port_period_names);
        mChartAllPeriodNames = res.getStringArray(R.array.quote_chart_all_period_names);
        mChartSupIndicator = getMainKTypes();
        mChartInfIndicator = getSubKTypes();
        setPopPeriodNames();
    }

    /**
     * 设置界面类型数据
     */
    private void setPopPeriodNames() {
        mPopPeriodNames = new DataItemResult();

        DataItemDetail item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_period_2_hours);
        mPopPeriodNames.addItem(item);

        item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_period_4_hours);
        mPopPeriodNames.addItem(item);

        item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_period_1_mins);
        mPopPeriodNames.addItem(item);

        item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_period_week);
        mPopPeriodNames.addItem(item);

        item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_period_month);
        mPopPeriodNames.addItem(item);

        item = new DataItemDetail();
        item.setIntValue("title", R.string.chart_type_detail);
        mPopPeriodNames.addItem(item);
    }

    /**
     * 返回主图指标
     *
     * @return
     */
    private String[] getMainKTypes() {
        JSONArray array = ConfigUtil.instance().mConfigObject.optJSONObject(ConfigType.INDICATOR_LIST_TYPE)
                .optJSONArray(ConfigType.INDICATOR_MIAN_TYPE);
        String[] arrayStr = null;
        if (null != array && array.length() > 0) {
            arrayStr = new String[array.length()];
            for (int i = 0; i < array.length(); i++) {
                arrayStr[i] = array.optString(i);
            }
        }

        return arrayStr;
    }

    /**
     * 返回副图指标
     *
     * @return
     */
    private String[] getSubKTypes() {
        JSONArray array = ConfigUtil.instance().mConfigObject.optJSONObject(ConfigType.INDICATOR_LIST_TYPE)
                .optJSONArray(ConfigType.INDICATOR_SUB_TYPE);
        String[] arrayStr = null;
        if (null != array && array.length() > 0) {
            arrayStr = new String[array.length()];
            for (int i = 0; i < array.length(); i++) {
                arrayStr[i] = array.optString(i);
            }
        }

        return arrayStr;
    }

    /**
     * 设置界面类型数据
     *
     * @param isSelect 产品是否已自选
     * @param isKType  是否是k线数据
     */
    public DataItemResult setMenuNames(int isSelect, boolean isKType, boolean isNeedShowProperty) {
        mMenuNames = new DataItemResult();
        DataItemDetail item;
        boolean isNight = true;//GTConfig.instance().isQuoteChartNight();//不用区分白天黑夜了
        if (isNeedShowProperty) {
            item = new DataItemDetail();
            item.setIntValue("title", R.string.chart_set_property);
            item.setIntValue("imageID", isNight ? R.mipmap.a_pop_night_contract : R.mipmap.a_pop_daytime_contract);
            mMenuNames.addItem(item);
        }

        if (isKType) {
            item = new DataItemDetail();
            item.setIntValue("title", R.string.chart_switch_type);
            item.setIntValue("imageID", isNight ? R.mipmap.a_pop_night_chart : R.mipmap.a_pop_daytime_chart);

            mMenuNames.addItem(item);

            item = new DataItemDetail();
            item.setIntValue("title", R.string.chart_set_new_price);
            if (GTConfig.instance().getBooleanValue(GTConfig.PREF_NEW_PRICE, false)) {
                item.setIntValue("imageID", R.mipmap.a_pop_daytime_checked);
                item.setBooleanValue("current_price_line", true);
            } else {
                item.setIntValue("imageID", isNight ? R.mipmap.a_pop_night_normal : R.mipmap.a_pop_daytime_normal);
                item.setBooleanValue("current_price_line", false);
            }
            mMenuNames.addItem(item);

            item = new DataItemDetail();
            item.setIntValue("title", R.string.chart_set_hollow_line);
            if (GTConfig.instance().getBooleanValue(GTConfig.PREF_HOLLOW_LINE, false)) {
                item.setIntValue("imageID", R.mipmap.a_pop_daytime_checked);
                item.setBooleanValue("hollow_line", true);
            } else {
                item.setIntValue("imageID", isNight ? R.mipmap.a_pop_night_normal : R.mipmap.a_pop_daytime_normal);
                item.setBooleanValue("hollow_line", false);
            }
            mMenuNames.addItem(item);
        }

        return mMenuNames;
    }

    /**
     * 返回主图指标名数组
     */
    public String[] getChartSupIndicator() {
        return mChartSupIndicator;
    }

    /**
     * 根据位置返回主图指标类型名
     *
     * @param index
     * @return
     */
    public String getChartSupIndicator(int index) {
        if (mChartSupIndicator == null)
            return "";

        if (index < 0 || index >= mChartSupIndicator.length)
            return "";

        return mChartSupIndicator[index];
    }

    /**
     * 返回副图指标数组
     */
    public String[] getChartInfIndicator() {
        return mChartInfIndicator;
    }

    /**
     * 根据位置返回副图指标类型名
     *
     * @param index
     * @return
     */
    public String getChartInfIndicator(int index) {
        if (mChartInfIndicator == null)
            return "";

        if (index < 0 || index >= mChartInfIndicator.length)
            return "";

        return mChartInfIndicator[index];
    }

    /**
     * 根据名称返回副图指标位置
     *
     * @return
     */
    public int getChartInfIndicator(String name) {
        if (mChartInfIndicator == null)
            return 0;

        for (int i = 0; i < mChartInfIndicator.length; i++) {
            if (mChartInfIndicator[i].equals(name)) {
                return i;
            }
        }

        return 0;
    }

    /**
     * 返回产品名数组
     */
    public String[] getLandPeriodNames() {
        return mLandPeriodNames;
    }

    /**
     * 根据位置返回日期周期文案
     *
     * @param index
     * @return
     */
    public String getLandPeriodName(int index) {
        if (mLandPeriodNames == null)
            return "";

        if (index < 0 || index >= mLandPeriodNames.length)
            return "";

        return mLandPeriodNames[index];
    }

    /**
     * 返回产品名数组
     */
    public String[] getPortPeriodNames() {
        return mPortPeriodNames;
    }

    /**
     * 根据位置返回日期周期文案
     *
     * @param index
     * @return
     */
    public String getPortPeriodName(int index) {
        if (mPortPeriodNames == null)
            return "";

        if (index < 0 || index >= mPortPeriodNames.length)
            return "";

        return mPortPeriodNames[index];
    }

    /**
     * 返回产品名数组
     */
    public DataItemResult getPopPeriodNames() {
        return mPopPeriodNames;
    }

    /**
     * 根据位置返回日期周期文案
     *
     * @param index
     * @return
     */
    public String getPopPeriodName(int index) {
        if (mPopPeriodNames == null)
            return "";

        if (index < 0 || index >= mPopPeriodNames.getDataCount())
            return "";

        return AppMain.getAppString(mPopPeriodNames.getItem(index).getInt("title"));
    }

    /**
     * 返回产品名数组
     */
    public DataItemResult getMenuNames() {
        return mMenuNames;
    }

    /**
     * 根据位置返回日期周期文案
     *
     * @param index
     * @return
     */
    public String getMenuName(int index) {
        if (mMenuNames == null)
            return "";

        if (index < 0 || index >= mMenuNames.getDataCount())
            return "";

        return AppMain.getAppString(mMenuNames.getItem(index).getInt("title"));
    }

    /**
     * 根据周期名返回id
     *
     * @param name
     * @return
     */
    public int getIdByPeriod(String name) {
        if (name.equals(AppMain.getAppString(R.string.chart_type_time))) {
            return ChartConfig.CHART_TYPE_TIME;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_1_mins))) {
            return ChartConfig.CHART_PERIOD_1_MINS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_5_mins))) {
            return ChartConfig.CHART_PERIOD_5_MINS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_15_mins))) {
            return ChartConfig.CHART_PERIOD_15_MINS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_30_mins))) {
            return ChartConfig.CHART_PERIOD_30_MINS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_60_mins))) {
            return ChartConfig.CHART_PERIOD_60_MINS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_2_hours))) {
            return ChartConfig.CHART_PERIOD_2_HOURS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_4_hours))) {
            return ChartConfig.CHART_PERIOD_4_HOURS;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_day))) {
            return ChartConfig.CHART_PERIOD_DAY;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_week))) {
            return ChartConfig.CHART_PERIOD_WEEK;
        } else if (name.equals(AppMain.getAppString(R.string.chart_period_month))) {
            return ChartConfig.CHART_PERIOD_MONTH;
        } else if (name.equals(AppMain.getAppString(R.string.chart_type_detail))) {
            return ChartConfig.CHART_TYPE_DETAILED;
        }

        return ChartConfig.CHART_TYPE_TIME;
    }

    /**
     * 根据id返回周期名
     *
     * @param id
     * @return
     */
    public String getPeriodById(int id) {
        if (id == ChartConfig.CHART_TYPE_TIME) {
            return AppMain.getAppString(R.string.chart_type_time);
        } else if (id == ChartConfig.CHART_PERIOD_1_MINS) {
            return AppMain.getAppString(R.string.chart_period_1_mins);
        } else if (id == ChartConfig.CHART_PERIOD_5_MINS) {
            return AppMain.getAppString(R.string.chart_period_5_mins);
        } else if (id == ChartConfig.CHART_PERIOD_15_MINS) {
            return AppMain.getAppString(R.string.chart_period_15_mins);
        } else if (id == ChartConfig.CHART_PERIOD_30_MINS) {
            return AppMain.getAppString(R.string.chart_period_30_mins);
        } else if (id == ChartConfig.CHART_PERIOD_60_MINS) {
            return AppMain.getAppString(R.string.chart_period_60_mins);
        } else if (id == ChartConfig.CHART_PERIOD_2_HOURS) {
            return AppMain.getAppString(R.string.chart_period_2_hours);
        } else if (id == ChartConfig.CHART_PERIOD_4_HOURS) {
            return AppMain.getAppString(R.string.chart_period_4_hours);
        } else if (id == ChartConfig.CHART_PERIOD_DAY) {
            return AppMain.getAppString(R.string.chart_period_day);
        } else if (id == ChartConfig.CHART_PERIOD_WEEK) {
            return AppMain.getAppString(R.string.chart_period_week);
        } else if (id == ChartConfig.CHART_PERIOD_MONTH) {
            return AppMain.getAppString(R.string.chart_period_month);
        } else if (id == ChartConfig.CHART_TYPE_DETAILED) {
            return AppMain.getAppString(R.string.chart_type_detail);
        }

        return AppMain.getAppString(R.string.chart_type_time);
    }

    public String[] getChartAllPeriodNames() {
        return mChartAllPeriodNames;
    }
}
